package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.StoreProductLogs;
import com.dingreading.cloud.admin.entity.table.StoreProductLogsTableDef;
import com.dingreading.cloud.admin.mapper.StoreProductLogsMapper;
import com.dingreading.cloud.admin.service.StoreProductLogsService;
import com.dingreading.cloud.common.util.NepUtil;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class StoreProductLogsServiceImpl extends BaseServiceImpl<StoreProductLogsMapper, StoreProductLogs, StoreProductLogsTableDef> implements StoreProductLogsService {

    @Override
    protected StoreProductLogsTableDef getTable() {
        return StoreProductLogsTableDef.storeProductLogs;
    }

    @Override
    public int dropByProductUid(String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.productUid.eq(uid))
                .and(table.source.eq(1));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public List<StoreProductLogs> listByStoreUidAndProductUid(String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.productUid.eq(uid));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.asc());

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public List<StoreProductLogs> listByProductUids(List<String> productUids) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.productUid.in(productUids));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.asc());

        return getMapper().selectListByCondition(condition);
    }


    @Override
    public boolean haveProjectUid(String projectUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.projectUid.eq(projectUid));

        return exists(condition);
    }

    @Override
    public void updateEnabledByProductUid(String productUid, Integer enabled, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.productUid.eq(productUid))
                .and(table.source.eq(1));

        UpdateChain.of(StoreProductLogs.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

}
